﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Club
{
    public partial class Booking : System.Web.UI.Page
    {
        BLL.BookFacility bookFacility = new BLL.BookFacility();
        BLL.BookTimeSlot timeSlot = new BLL.BookTimeSlot();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, object> sessionVar = (Dictionary<string, object>)Session["vars"];
                DAL.User user = (DAL.User)sessionVar["userVar"];
                if (user == null)
                {
                    Response.Redirect("Default.aspx?prepage=Booking.aspx");
                }

                if (!IsPostBack)
                {
                    DropDownList1.DataSource = bookFacility.getFacilitiesList();
                    DropDownList1.DataTextField = "Name";
                    DropDownList1.DataValueField = "Id";
                    DropDownList1.DataBind();
                    DAL.Facility facility = (DAL.Facility)sessionVar["facility"];

                    if (facility != null)
                    {
                        int flag = 0;
                        for (int i = 0; i < DropDownList1.Items.Count; i++)
                        {
                            if (DropDownList1.Items[i].Value.ToString() == facility.Id.ToString())
                            {
                                flag = i;
                                break;
                            }
                        }
                        DropDownList1.Items[flag].Selected = true;
                    }
                    else
                    {
                        DropDownList1.Items[0].Selected = true;
                    }
                    TextBox1.Text = DateTime.Today.Year + "/" + DateTime.Today.Month + "/" + DateTime.Today.Day;
                    DAL.Facility temp = new DAL.Facility();
                    DAL.Booking book = new DAL.Booking();
                    book.BookingDate = Convert.ToDateTime(TextBox1.Text);
                    book.FacilityId = temp.getFacility(DropDownList1.Items[0].Text).Id;
                    DropDownList2.DataSource = timeSlot.getAvailableTimeSlots(book);
                    DropDownList2.DataBind();

                }
                if (Request.QueryString["msg"] != null)
                {
                    Label3.Text = Request.QueryString["msg"].ToString();
                }
            }
            catch (Exception ex)
            {
                WebCommon.ShowMsg(Response, ex.Message);
            }
            
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            DAL.Booking book = new DAL.Booking();
            book.BookingDate = Convert.ToDateTime(TextBox1.Text);
            DAL.Facility facility = new DAL.Facility();
            facility = facility.getFacility(DropDownList1.SelectedItem.Text);
            book.FacilityId = facility.Id;
            DropDownList2.DataSource = timeSlot.getAvailableTimeSlots(book);
            DropDownList2.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DAL.Booking book = new DAL.Booking();
            book.BookingDate = Convert.ToDateTime(TextBox1.Text);
            book.BookingTime = DropDownList2.SelectedValue;
            DAL.Facility facility = new DAL.Facility();
            facility = facility.getFacility(DropDownList1.SelectedItem.Text);
            book.FacilityId = facility.Id;
            book.UserId = ((DAL.User)(((Dictionary<string, object>)Session["vars"])["userVar"])).Id;//dummy user id
            bookFacility.placeBook(book);
            Response.Redirect("~/Booking.aspx?msg=Book successfully.");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DAL.Booking book = new DAL.Booking();
            book.BookingDate = Convert.ToDateTime(TextBox1.Text);
            DAL.Facility facility = new DAL.Facility();
            facility = facility.getFacility(DropDownList1.SelectedItem.Text);
            book.FacilityId = facility.Id;
            DropDownList2.DataSource = timeSlot.getAvailableTimeSlots(book);
            DropDownList2.DataBind();
        }
    }
}